package com.fau.wm.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

/**
 * @desc 储位-层位表
 * @author liy
 * @datetime 2014年7月27日,下午4:32:37
 */
@Entity
@Table(name = "medical_layer_position")
public class MedicalLayerPosition implements Serializable ,JSONAware{

	private static final long serialVersionUID = 5520606245792893804L;
	
	private String id;
	private String positionNum;		// 层位号
	private String layerId;			// 层id
	private String operator;		// 操作人姓名
	private String hospitalId;		// 所属公司
	private Date createTime = new Date();	// 创建时间
	private Date updateTime = new Date();	// 更新时间
	private MedicalShelfLayer shelfLayer;
	private Set<MedicalMedicine> medicineSets;
	
	private String warehouseId;		// 仓库
	private String shelfNum;		// 货架
	private String layer;			// 层
	private String medicals;		// 位所关联的药品
	private String serialNum;
	
	public MedicalLayerPosition() {}
	
	public MedicalLayerPosition(String positionNum, MedicalShelfLayer shelfLayer) {
		this.positionNum = positionNum;
		this.shelfLayer = shelfLayer;
	}
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "position_num", length = 30)
	public String getPositionNum() {
		return positionNum;
	}

	public void setPositionNum(String positionNum) {
		this.positionNum = positionNum;
	}

	@Transient
	public String getLayerId() {
		return layerId;
	}

	public void setLayerId(String layerId) {
		this.layerId = layerId;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "medical_medicine_position", joinColumns = {@JoinColumn(name = "position_id")}, inverseJoinColumns = {@JoinColumn(name = "medical_id")})
	public Set<MedicalMedicine> getMedicineSets() {
		return medicineSets;
	}

	public void setMedicineSets(Set<MedicalMedicine> medicineSets) {
		this.medicineSets = medicineSets;
	}

	@ManyToOne
	@JoinColumn(name = "layer_id")
	public MedicalShelfLayer getShelfLayer() {
		return shelfLayer;
	}

	public void setShelfLayer(MedicalShelfLayer shelfLayer) {
		this.shelfLayer = shelfLayer;
	}

	@Column(name = "operator", length = 15)
	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Transient
	public String getLayer() {
		return layer;
	}

	public void setLayer(String layer) {
		this.layer = layer;
	}

	@Transient
	public String getMedicals() {
		return medicals;
	}

	public void setMedicals(String medicals) {
		this.medicals = medicals;
	}

	@Transient
	public String getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(String warehouseId) {
		this.warehouseId = warehouseId;
	}

	@Transient
	public String getShelfNum() {
		return shelfNum;
	}

	public void setShelfNum(String shelfNum) {
		this.shelfNum = shelfNum;
	}
	
	@Column(name = "hospital_id", length = 32)
	public String getHospitalId() {
		return hospitalId;
	}

	public void setHospitalId(String hospitalId) {
		this.hospitalId = hospitalId;
	}
	
	@Transient
	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("positionNum", positionNum);
		obj.put("layer", shelfLayer.getLayerNum());
		obj.put("shelfNum", shelfLayer.getShelf().getShelfNum());
		obj.put("warehouseId", shelfLayer.getShelf().getWarehouse().getId());
		obj.put("warehouseName", shelfLayer.getShelf().getWarehouse().getName());
		obj.put("warehouseCode", shelfLayer.getShelf().getWarehouse().getCode());
		obj.put("positionName", shelfLayer.getShelf().getShelfNum()+"架-"
				+shelfLayer.getLayerNum()+"层-"
				+positionNum+"位");
		StringBuffer buffer = new StringBuffer();
		StringBuffer buffer2 = new StringBuffer();
		for (MedicalMedicine medicalMedicine : medicineSets) {
			buffer.append(medicalMedicine.getName()+",");
			buffer2.append(medicalMedicine.getId()+",");
		}
		if (buffer.length() > 0 && buffer2.length() > 0) {
			buffer.deleteCharAt(buffer.length()-1);
			buffer2.deleteCharAt(buffer2.length()-1);
		}
		obj.put("medicineSets", buffer.toString());
		obj.put("medicals", buffer2.toString());
		obj.put("createTime", DateFormatUtils.format(createTime,"yyyy-MM-dd HH:mm:ss"));
		obj.put("updateTime", DateFormatUtils.format(updateTime,"yyyy-MM-dd HH:mm:ss"));
		return obj.toString();
	}
	
}
